<template>
	<view class="page">
		<!-- 支付成功 -->
		<view v-if="isPaySuccess === 1" class="flex-col">
			<view class="flex-col section_1">
				<view class="flex-col success-group">
					<image
						src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844621395277574.png"
						class="image_1" />
					<view class="flex-col items-center group_1">
						<text class="text">支付成功</text>
						<text class="text_1">您已支付成功，请去订单查看</text>
					</view>
				</view>
				<view class="section_2"> </view>
			</view>
			<view class="flex-col section_3">
				<view class="group_2">
					<text class="text_2">￥</text>
					<text class="text_3">{{ pay_money }}</text>
				</view>
				<view class="flex-col group_3">
					<view class="flex-row">
						<text class="text_4">支付方式</text>
						<text class="text_5">{{ payType }}</text>
					</view>
					<view class="flex-row group_5">
						<text class="text_6">支付单号</text>
						<text class="text_7">{{ order_sn }}</text>
					</view>
					<view class="flex-row group_6">
						<text class="text_8">支付时间</text>
						<text class="text_9">{{ create_time }}</text>
					</view>
				</view>
				<view class="flex-col group_7">
					<view class="flex-col items-center button" @click="toPath">
						<text>返回商户</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 支付失败 -->
		<view v-if="isPaySuccess === 2" class="flex-col fail-group">
			<view class="flex-col fail-section_1">
				<image
					src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844631489950773.png"
					class="image_1" />
				<view class="flex-col items-center group_1">
					<text class="text">支付失败</text>
					<text class="text_1">支付失败请重新支付订单</text>
				</view>
			</view>
			<view class="flex-col group_2">
				<view class="flex-col items-center button">
					<text @click="reloadPay">重新支付</text>
				</view>
				<view class="flex-col items-center button_1" @click="toPath">
					<text>返回商户</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				payType: '微信支付',
				order_sn: 'O202503285350995968',
				create_time: '2025-03-28 15:07:17',
				pay_money: '264.00',
				isPaySuccess: 0,
				options: {},
				openid: ''
			};
		},
		onLoad(e) {
			this.options = e
			this.order_sn = this.options.order_sn
			this.pay_money = this.options.pay_money
			// console.log(e);
			// uni.showToast({
			// 	icon: 'none',
			// 	title: e.order_sn
			// })
			
			// this.wxlogin()
		},
		methods: {
			time(){
				let date = new Date()
				let create_time = date.getFullYear() + '-' + 
													this.fill(Number(date.getMonth()) + 1) + '-' + 
													this.fill(date.getDate()) +' '+ 
													this.fill(date.getHours()) + ':' + 
													this.fill(date.getMinutes()) + ':' + 
													this.fill(date.getDate());
				this.create_time = create_time
			},
			fill(a){
				return 10 > a ? '0' + (0 | a) : a
			},
			reloadPay(){
				uni.showLoading({
					title:'loading...',
					mask: true
				});
				this.payment(this.openid)
			},
			// 小程序后拉起支付
			payment(openid){
				this.$http.http('api/wxapp/pay',{openid,order_sn: this.options.order_sn},'POST').then(res=>{
					// console.log('payment', res);
					wx.requestPayment({
					  timeStamp: res.data.timeStamp,
					  nonceStr: res.data.nonceStr,
					  package: res.data.package,
					  signType: res.data.signType,
					  paySign: res.data.paySign,
					  success: (res)=> {
							this.isPaySuccess = 1
							this.time()
							console.log('success', res);
						},
					  fail: (res)=> {
							this.isPaySuccess = 2
							// {errMsg: "requestPayment:fail cancel"}
							console.log('fail', res);
						},
						complete() {
							uni.hideLoading()
						}
					})
				}).catch(res=>{
					uni.hideLoading()
				})
			},
			async getOpenid(code){
				const res = await this.$http.http('api/wxapp/login',{code},'POST')
				if(res.code === 1){
					const msg = JSON.parse(res.msg)
					uni.setStorageSync('openid', msg.openid)
					uni.setStorageSync('session_key', msg.session_key)
					return msg
				}
			},
			wxlogin(){
				uni.showLoading({
					title:'loading...',
					mask: true
				});
				wx.login({
				  success: (res)=> {
				    if (res.code) {
							this.getOpenid(res.code).then(msg => {
								// console.log('msg', msg);
								this.openid = msg.openid
								this.payment(msg.openid)
							})
				    } else {
				      // console.log('登录失败！' + res.errMsg)
							uni.hideLoading()
				    }
				  },
					fail() {
						uni.hideLoading()
					}
				})
			},
			init() {
				let order_info = uni.getStorageSync('order_info');
				for (let i in order_info) {
					this[i] = order_info[i];
				}
			},
			toPath() {
				uni.reLaunch({
					url: '/pages/index/home'
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	@import "../../styles/payment.scss";
</style>